﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Youtube_HD.Model;

namespace Youtube_HD.Services
{
    public class PlayListViewModel :ViewModelBase, IPlayListViewModel
    {
        
        ObservableCollection<YoutubeVideo> youtubeVideos;
        public ObservableCollection<YoutubeVideo> YoutubeVideos
        {
            get
            {
                if (youtubeVideos == null)
                    youtubeVideos = new ObservableCollection<YoutubeVideo>();
                return youtubeVideos;
            }
            set
            {
                youtubeVideos = value;
                RaisePropertyChanged(() => YoutubeVideos);
            }
        }
        ObservableCollection<YoutubeVideo> youtubeVideosListRelated;
        public ObservableCollection<YoutubeVideo> YoutubeVideosListRelated
        {
            get
            {
                if (youtubeVideosListRelated == null)
                    youtubeVideosListRelated = new ObservableCollection<YoutubeVideo>();
                return youtubeVideosListRelated;
            }
            set
            {
                youtubeVideosListRelated = value;
                RaisePropertyChanged(() => YoutubeVideosListRelated);
            }
        }
        IHttpChannel httpChannel;
        public PlayListViewModel(IHttpChannel httpChannel)
        {
            this.httpChannel = httpChannel;
        }
        public async Task<Model.feed> GetListReletedPlayList(string playListId)
        {
            string urlData = "https://gdata.youtube.com/feeds/api/videos/" + playListId + "/related?v=2";
            var getData = await httpChannel.GetDataYoutubeAsync(urlData);
            if (getData != null)
            {
                var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                if (data.entry.Count() > 0)
                {
                    foreach (var u in data.entry)
                    {
                        var duration = u.group.duration.seconds;
                        string a = u.link[0].href.ToString().Remove(0, 32);
                        var youtubeList = new YoutubeVideo()
                        {
                            Description = u.group.description.Value,
                            PubDate = "Published: " + u.published.ToString("dd-mm-yyyy"),
                            Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/sddefault.jpg",
                            Title = u.title,
                            Id = a.Substring(0, 11),
                            Author = "Author: " + u.author.name,
                            ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                            Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString()
                            ,
                            Like = u.rating1.numLikes,
                            Unlike = u.rating1.numDislikes
                        };
                        YoutubeVideosListRelated.Add(youtubeList);
                    }
                }
            }
            else
            {
                new MessageDialog("Playlist not found").ShowAsync();
            }
            return null;
        }
        public async Task<Model.feed> GetListDetailPlayList(string playListId)
        {
            string urlData = "https://gdata.youtube.com/feeds/api/playlists/" + playListId + "?start-index=1&max-results=50&v=2";
            var getData = await httpChannel.GetDataYoutubeAsync(urlData);
            if (getData != null)
            {
                var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                if (data.entry.Count() > 0)
                {
                    foreach (var u in data.entry)
                    {
                        var duration = u.group.duration.seconds;
                        string a = u.link[0].href.ToString().Remove(0, 32);
                        var youtubeList = new YoutubeVideo()
                        {
                            Description = u.group.description.Value,
                            PubDate = "Published: " + u.published.ToString(),
                            Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/sddefault.jpg",
                            Title = u.title,
                            Id = a.Substring(0, 11),
                            Author = "Author: " + u.author.name,
                            ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                            Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString()
                            ,
                            Like = u.rating1.numLikes,
                            Unlike = u.rating1.numDislikes
                        };
                        YoutubeVideos.Add(youtubeList);
                    }
                }
            }
            else
            {
                new MessageDialog("Playlist not found").ShowAsync();
            }
            return null;
        }
    }
}
